<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Pendataan_Guru extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_guru';

		$this->primary_key = 'aka_guru.guru_id';

	}
	function get_pendataan_guru($departemen_id,$rapor_id,$pelajaran_id,$aktif=null){
		$this->load->database();	
		$filter = "";
			
			if($aktif != null)
				$filter = "AND {$this->table_name}.aktif='$aktif'";
		
			   $sql = "SELECT aka_guru.*,mst_pegawai.nip, mst_pegawai.nama, aka_pelajaran.nama as nama_pelajaran, aka_status_guru.status FROM aka_guru JOIN mst_pegawai ON aka_guru.pegawai_id=mst_pegawai.pegawai_id JOIN aka_pelajaran ON aka_guru.pelajaran_id=aka_pelajaran.pelajaran_id JOIN aka_status_guru ON aka_guru.status_guru_id=aka_status_guru.status_guru_id where aka_guru.departemen_id='$departemen_id' and aka_guru.kategori_rapor_id='$rapor_id' and aka_guru.pelajaran_id='$pelajaran_id' $filter ORDER BY aka_guru.guru_id;"; 
			    // echo $sql = "select * from aka_guru where aka_guru.departemen_id='$departemen_id' and aka_guru.kategori_rapor_id='$rapor_id' and aka_guru.pelajaran_id='$pelajaran_id' $filter order by aka_guru.guru_id";
			  
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_pendataan_guru_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM aka_guru WHERE guru_id='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	function get_all(){
		$this->load->database();		
		 $sql = "SELECT aka_guru.*,mst_pegawai.nip, mst_pegawai.nama, aka_pelajaran.nama as nama_pelajaran, aka_status_guru.status FROM aka_guru JOIN mst_pegawai ON aka_guru.pegawai_id=mst_pegawai.pegawai_id JOIN aka_pelajaran ON aka_guru.pelajaran_id=aka_pelajaran.pelajaran_id JOIN aka_status_guru ON aka_guru.status_guru_id=aka_status_guru.status_guru_id JOIN aka_kategori_rapor ON aka_guru.kategori_rapor_id=aka_kategori_rapor.kategori_rapor_id  ORDER BY aka_guru.guru_id;";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert('aka_guru',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where('guru_id',$id);
				$this->db->update('aka_guru',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM aka_guru WHERE guru_id='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>